package com.example.demo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.demo.Common.CommonIT;
import com.example.demo.Excel.ExcelUtils;
import com.example.demo.VO.PermissionsVO;
import com.example.demo.annotation.AuthToken;
import com.example.demo.entity.Permissions;
import com.example.demo.service.IPermissionsService;
import com.xc.roadRisk.common.utils.ResponseUtils;
import com.xc.roadRisk.common.vo.PageList;
import com.xc.roadRisk.common.vo.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <p>
*  前端控制器
* </p>
*
* @author hanfei
* @since 2020-04-08
*/
@Api(value = "权限",tags = "权限")
@RestController
public class PermissionsController {
   @Autowired
   private IPermissionsService permissionsApi;

   @ApiOperation("创建单个Permissions")
   @ApiImplicitParam(name = "version", paramType = "path")
   @PostMapping("pv/permissionss")
   @AuthToken
   public Response<Boolean> insert(@RequestBody Permissions permissions) {
       CommonIT commonIT=new CommonIT();
       String id=commonIT.findId();
       permissions.setId(id);
       Response<Boolean> response=ResponseUtils.returnObjectSuccess(permissionsApi.insert(permissions));
       response.setMsg(id);
       return response;
   }

   @ApiOperation("编辑单个Permissions")
   @ApiImplicitParam(name = "version", paramType = "path")
   @PutMapping("pv/permissionss/{uid}")
   @AuthToken
   public Response<Boolean> updateById(@PathVariable("uid") String uid, @RequestBody Permissions permissions) {
       permissions.setId(uid);

       return ResponseUtils.returnObjectSuccess(permissionsApi.updateById(permissions));
   }
   @ApiOperation("查询单个Permissions")
   @ApiImplicitParam(name = "version", paramType = "path")
   @GetMapping("pb/permissionss/{uid}")
   @AuthToken
   public Response<Permissions> selectById(@PathVariable("uid") String uid) {
       return ResponseUtils.returnObjectSuccess(permissionsApi.selectById(uid));
   }

   @ApiOperation("查询分页Permissions")
   @ApiImplicitParam(name = "version", paramType = "path")
   @GetMapping("pb/permissionss/action/search")
   @AuthToken
   public Response<PageList<Permissions>> selectPage(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                     Permissions permissions) {
       Page<Permissions> page = permissionsApi.selectPage(new Page<>(pageNo, pageSize), new EntityWrapper<>(permissions));
       return ResponseUtils.returnObjectSuccess(new PageList<>(page));
   }
   @ApiOperation("模糊查询分页")
   @ApiImplicitParam(name = "version", paramType = "path")
   @GetMapping("pb/permissionss/action/findByKey")
   @AuthToken
   public Response<PageList<Permissions>> findByKey(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                    @RequestParam(value = "key", required = false) String key) {
       Page<Permissions> page = permissionsApi.findByKey(new Page<>(pageNo, pageSize), key);
       return ResponseUtils.returnObjectSuccess(new PageList<>(page));
       }

   @ApiOperation("查询所有")
   @ApiImplicitParam(name = "version", paramType = "path")
   @GetMapping("pb/permissionss/findall")
   @AuthToken
   public Response<List<Permissions>> findall(Permissions permissions) {
       return ResponseUtils.returnObjectSuccess(permissionsApi.selectList(new EntityWrapper<>(permissions)));
       }

   @ApiOperation("导出")
   @GetMapping("/permissionss/export")
   @AuthToken
   public String exportAll(HttpServletResponse response,Permissions permissions) {
       List<Permissions> list=permissionsApi.selectList(new EntityWrapper<>(permissions));
       ExcelUtils.writeExcel(response,list,Permissions.class);
       return null;
       }
   @ApiOperation("导入")
   @PostMapping("/permissionss/import")
   @AuthToken
   public Response importAll(@RequestParam("file")MultipartFile file){
       List<Permissions> list=ExcelUtils.readExcel(null,Permissions.class,file);
       for(Permissions a:list){
       CommonIT commonIT=new CommonIT();
       String id=commonIT.findId();
       a.setId(id);

       }

       return ResponseUtils.returnObjectSuccess(permissionsApi.insertBatch(list));
       }
@ApiOperation("模糊查询加定向查询分页")
@ApiImplicitParam(name = "version", paramType = "path")
@GetMapping("pb/permissionss/action/findByKeyAndAll")
@AuthToken
public Response<PageList<Permissions>> findByKeyAndAll(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                       @RequestParam(value = "key",required = false) String key,
                                                       Permissions entity) {
       Page<Permissions> page = permissionsApi.findByKeyAndAll(new Page<>(pageNo, pageSize), key,entity);
       return ResponseUtils.returnObjectSuccess(new PageList<>(page));
       }

    @ApiOperation(value = "权限树形结构", notes = "权限树形结构")
    @GetMapping(value = "pb/permissionss/action/shuxing")
    @AuthToken
    public Response shuxingjiegou() {
       Response response=new Response();
       List<PermissionsVO> rootMenu=permissionsApi.findall();
        List<PermissionsVO> menuList = new ArrayList<PermissionsVO>();
        for (int i = 0; i < rootMenu.size(); i++) {
            // 一级菜单没有parentId
            if (StringUtils.isBlank(rootMenu.get(i).getPid())) {
                menuList.add(rootMenu.get(i));
            }
        }
        // 为一级菜单设置子菜单，getChild是递归调用的
        for (PermissionsVO menu : menuList) {
            menu.setChildTDPermissions(permissionsApi.getChild(menu.getId(), rootMenu));
        }
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("PermissionsList", menuList);
        response.setData(jsonMap);
        response.setCode("200");
        return response;
    }
    }

